package com.nickstumpos.android.easyVolume.preset.presentation;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.RelativeLayout;

import com.nickstumpos.android.easyVolume.R;
import com.nickstumpos.android.easyVolume.constants.EVCONSTANTS;
import com.nickstumpos.android.easyVolume.events.presentation.EventTriggeredPresetActivity;
import com.nickstumpos.android.easyVolume.preset.service.PresetService;
import com.nickstumpos.android.easyVolume.preset.types.PresetIdentifier;
import com.nickstumpos.android.easyVolume.scheduler.presentation.SchedulePresetActivity;
import com.nickstumpos.android.easyVolume.streams.presentation.types.Screen;
import com.nickstumpos.android.easyVolume.volume.controller.IVolumeController;

public class PresetScreen extends RelativeLayout implements OnClickListener,
		OnLongClickListener {

	private Activity			main;
	private PresetButton	presetOne;
	private PresetButton	presetTwo;
	private PresetButton	presetThree;
	private PresetButton	presetFour;
	private PresetButton	presetFive;
	private PresetButton	presetSix;
	private PresetButton	schedulePresets;
	private PresetButton	eventPreset;			
	private PresetService	presetService;

	public PresetScreen(Context context) {
		super(context);
		this.presetService = new PresetService(context);
	}

	public PresetScreen(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.presetService = new PresetService(context);
	}

	public PresetScreen(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.presetService = new PresetService(context);
	}

	public void assignPresetButtons(Activity main) {
		this.presetOne = (PresetButton) main.findViewById(R.id.PresetOne);
		this.presetTwo = (PresetButton) main.findViewById(R.id.PresetTwo);
		this.presetThree = (PresetButton) main.findViewById(R.id.PresetThree);
		this.presetFour = (PresetButton) main.findViewById(R.id.PresetFour);
		this.presetFive = (PresetButton) main.findViewById(R.id.PresetFive);
		this.presetSix = (PresetButton) main.findViewById(R.id.PresetSix);
		this.eventPreset = (PresetButton) main.findViewById(R.id.ScheduleEvents);
		this.schedulePresets = (PresetButton) main
				.findViewById(R.id.SchedulePresets);
		this.presetOne.setButtonName(PresetIdentifier.ONE);
		this.presetTwo.setButtonName(PresetIdentifier.TWO);
		this.presetThree.setButtonName(PresetIdentifier.THREE);
		this.presetFour.setButtonName(PresetIdentifier.FOUR);
		this.presetFive.setButtonName(PresetIdentifier.FIVE);
		this.presetSix.setButtonName(PresetIdentifier.SIX);
		this.schedulePresets.setButtonName(PresetIdentifier.SCHEDULE);

		this.presetOne.setOnClickListener(this);
		this.presetTwo.setOnClickListener(this);
		this.presetThree.setOnClickListener(this);
		this.presetFour.setOnClickListener(this);
		this.presetFive.setOnClickListener(this);
		this.presetSix.setOnClickListener(this);
		this.schedulePresets.setOnClickListener(this);
		this.presetOne.setOnLongClickListener(this);
		this.presetTwo.setOnLongClickListener(this);
		this.presetThree.setOnLongClickListener(this);
		this.presetFour.setOnLongClickListener(this);
		this.presetFive.setOnLongClickListener(this);
		this.presetSix.setOnLongClickListener(this);
		this.schedulePresets.setOnLongClickListener(this);
		this.refreshButtonTitles();
		this.schedulePresets.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(PresetScreen.this.getContext(),
						SchedulePresetActivity.class);
				PresetScreen.this.main.startActivityForResult(i,
						EVCONSTANTS.SCHED_RETURN);

			}

		});
		this.eventPreset.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(PresetScreen.this.getContext(),
						EventTriggeredPresetActivity.class);
				PresetScreen.this.main.startActivityForResult(i,
						EVCONSTANTS.SCHED_RETURN);

			}

		});
		this.main = main;
	}

	@Override
	public void onClick(View v) {
		if (v.getClass().equals(PresetButton.class)) {
			if (((PresetButton) v).getButtonName().equals(PresetIdentifier.SCHEDULE)) {
				this.scheduleButtonPressed();
			} else {
				this.presetButtonPressed(((PresetButton) v).getButtonName());
			}
		}
	}

	@Override
	public boolean onLongClick(View v) {
		if (v.getClass().equals(PresetButton.class)) {
			if (!((PresetButton) v).getButtonName().equals(PresetIdentifier.SCHEDULE)) {
				this.shouldSetPreset(((PresetButton) v).getButtonName());

			}
		}
		return true;
	}

	private void presetButtonPressed(PresetIdentifier number) {
		if (this.presetService.isSet(number)) {
			this.presetService.applyPreset(number);
		} else {
			this.shouldFirstSet(number);
		}
	}

	public void refreshButtonTitles() {
		this.presetOne.updateButtonTitle(this.presetService
				.getPresetTitle(PresetIdentifier.ONE));
		this.presetTwo.updateButtonTitle(this.presetService
				.getPresetTitle(PresetIdentifier.TWO));
		this.presetThree.updateButtonTitle(this.presetService
				.getPresetTitle(PresetIdentifier.THREE));
		this.presetFour.updateButtonTitle(this.presetService
				.getPresetTitle(PresetIdentifier.FOUR));
		this.presetFive.updateButtonTitle(this.presetService
				.getPresetTitle(PresetIdentifier.FIVE));
		this.presetSix.updateButtonTitle(this.presetService
				.getPresetTitle(PresetIdentifier.SIX));
	}

	public void registerScreen(IVolumeController screen, Screen type) {
		this.presetService.getStreams().put(type, screen);
	}

	private void scheduleButtonPressed() {

	}

	private void shouldFirstSet(final PresetIdentifier number) {
		AlertDialog ad = new AlertDialog.Builder(this.getContext())
				.setTitle("Confirm Set Preset")
				.setMessage(
						"This preset has not yet been set. Click OK to set the current volume as this preset")
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int whichButton) {
						PresetScreen.this.presetService.setPreset(number);
					}
				}).setNegativeButton("No Thanks",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog, int whichButton) {
							}
						}).create();
		ad.show();
	}

	private void shouldSetPreset(final PresetIdentifier number) {
		AlertDialog ad = new AlertDialog.Builder(this.getContext()).setTitle(
				"Confirm Set Preset").setMessage(
				"Click OK to set the current volume as this preset").setPositiveButton(
				"OK", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int whichButton) {
						PresetScreen.this.presetService.setPreset(number);
					}
				}).setNegativeButton("No Thanks",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int whichButton) {
					}
				}).setNeutralButton("Rename Preset",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int whichButton) {
						Intent i = new Intent(PresetScreen.this.getContext(),
								TextEntryActivity.class);
						Bundle bundle = new Bundle();
						bundle.putSerializable(EVCONSTANTS.CURRENT_EDIT_FIELD, number);
						i.putExtras(bundle);
						PresetScreen.this.main.startActivityForResult(i,
								EVCONSTANTS.EDIT_TEXT_RETURN);

					}
				}).create();
		ad.show();
	}

}
